﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HardwareAs
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] != null) //If User Login Successfully
            {
                Button1.Text = "Log out";
                Button2.Visible = false;
                Label1.Text = "Welcome " + Session["username"];

            }
            else //If User Haven't Login
            {
                Button2.Visible = true;
                if (!Request.RawUrl.Contains("Login.aspx") && !Request.RawUrl.Contains("Register.aspx"))
                {
                    Response.Write("<script>alert('You are not login yet!')</script>");
                    String url=ResolveUrl("~")+"Account/Login.aspx";
                    Response.Write("<script>window.location.href='"+url+"'</script>");
               //     Response.Redirect();
                    return;
                }

            }
            //If User Does not a Administrator Return to the refer page
            if (Session["username"]!=null && Session["admin"]==null && Request.RawUrl.Contains("admin"))
            {
                Response.Write("<script>alert('You are not administrator!')</script>");
                Response.Write("<script>window.location.href='" + Request.UrlReferrer.OriginalString + "'</script>");
                return;
            
            }
            // For the sake of simplicity, the admin does not have access to normal user functionalities
            if (Session["admin"] != null && !Request.RawUrl.Contains("admin"))
            {
                Response.Write("<script>alert('For the sake of simplicity, the admin does not have access to normal user functionalities')</script>");
                Response.Write("<script>window.location.href='" + Request.UrlReferrer.OriginalString + "'</script>");
                return;
            
            }
          
        
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //If User Session doesn't exist this button become "Register" Otherwise "Log Out"
            if (Session["username"] != null)
            {
                Session.Clear();
               
                Label1.Text = "";
                Button1.Text = "Register";
                Response.Redirect("~/Account/Login.aspx");
                return;
            }
            Response.Redirect("~/Account/Register.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e) //Login Button Event
        {
            Response.Redirect("~/Account/Login.aspx");
        }
    }
}
